package com.ollobot.authorization.security;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

public class EmailVerificationAuthenticationToken extends AbstractAuthenticationToken {

    private final Object principal;
    private Object credentials;
    private final String email;
    private final String code;
    private final String firstName;
    private final String lastName;

    // Constructor for unauthenticated token (before verification)
    public EmailVerificationAuthenticationToken(String email, String code, String firstName, String lastName) {
        super(null);
        this.principal = email;
        this.credentials = code;
        this.email = email;
        this.code = code;
        this.firstName = firstName;
        this.lastName = lastName;
        setAuthenticated(false);
    }

    // Constructor for authenticated token (after verification)
    public EmailVerificationAuthenticationToken(Object principal, Object credentials,
                                               Collection<? extends GrantedAuthority> authorities,
                                               String email, String code, String firstName, String lastName) {
        super(authorities);
        this.principal = principal;
        this.credentials = credentials;
        this.email = email;
        this.code = code;
        this.firstName = firstName;
        this.lastName = lastName;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return credentials;
    }

    @Override
    public Object getPrincipal() {
        return principal;
    }

    public String getEmail() {
        return email;
    }

    public String getCode() {
        return code;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }



    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException(
                "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }
        super.setAuthenticated(false);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
        credentials = null;
    }
}
